<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/26
  Time: 16:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
       <base href="<%=basePath%>">
       <title>商品信息</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
</head>

  <body>

 <%
       response.setCharacterEncoding("UTF-8");

       request.setCharacterEncoding("UTF-8");

       String id = request.getParameter("id");
       String name = request.getParameter("name");
       String price = request.getParameter("price");
       String number = request.getParameter("number");
       String comment = request.getParameter("comment");

       Connection conn = null;
       Statement stat = null;
       ResultSet rs = null;

       Class.forName("com.mysql.cj.jdbc.Driver");
       String url = "jdbc:mysql://localhost:3306/demo";
       String user = "root";
       String password = "123456";
       conn = DriverManager.getConnection(url,user,password);
       stat = conn.createStatement();

       rs = stat.executeQuery("select * from goods");
%>



<%----%>
<%
    if("zhenjia".equals(session.getAttribute("username").toString())){
%>
    <br>
    <h2>商品信息</h2>  
    <hr>    
    <br>
    <h3>【<%=session.getAttribute("username") %>】数据库</h3>
    <br>


  <h3>全部商品信息如下</h3>
   <table width="450" border="100" cellSpacing=3 style="font-size:15pt;border:dashed 1pt">

       <tr>
       <td>id号</td>
       <td>名称</td>
       <td>价格</td>
       <td>数量</td>
       <td>备注</td>
       </tr>

       <%
       while(rs.next()) {
       out.print("<tr>");
       out.print("<td>" + rs.getInt("id") + "</td>");
       out.print("<td>" + rs.getString("name") + "</td>");
       out.print("<td>" + rs.getDouble("price") + "</td>");
       out.print("<td>" + rs.getInt("number") + "</td>");
       out.print("<td>" + rs.getString("comment") + "</td>");
       out.print("</tr>");
       }
       %>
         </table>
      <br>

    <form action="select_for_id.jsp" method="post">
      <h3>按id号查询:<input type="text" name="id"  value="" title="id号不能为空" ></input>
       <input type="submit" value="查询"/></h3>
       <br>
    </form>

    <form action="select_for_name.jsp" method="post">
        <h3>按名称查询:<input type="text" name="name" value="" title="名称不能为空"></input>
       <input type="submit" value="查询" /></h3>
       <br>
    </form>

    <form action="select_for_price.jsp" method="post">
       <h3> 按价格查询:<input type="text" name="price" value="" title="价格不能为空"></input>
       <input type="submit" value="查询"/></h3>
       <br>
    </form>

    <form action="select_for_number.jsp" method="post">
       <h3>  按数量查询:<input type="text" name="number" value="" title="数量不能为空"></input>
       <input type="submit" value="查询"/></h3>
       <br>
    </form>

   
    <form action="select_for_comment.jsp" method="post">
        <h3> 按备注查询:<input type="text" name="comment" value="" title="备注不能为空"></input>
       <input type="submit" value="查询"/></h3>
       <br>
    </form>

      <%

       if(rs != null) {
          rs.close();
          rs = null;
       }

       if(stat != null) {
          stat.close();
          stat = null;
       }

          if(conn != null) {
          conn.close();
          conn = null;
       }

       %>
    <%
    }else{
        out.print("<script>alert('数据库未成功登录！');window.location='http://localhost:8080/index.html';</script>");
    }
    %>

  </body>

</html>
